{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "## Линейные и угловые скорости манипулятора из Лаб №3\n",
    "\n",
    "![screenshot](../static/lab3.jpg)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### Импорты"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from math import pi\n",
    "\n",
    "import sympy as sy\n",
    "\n",
    "from robobase.forwardkinematics import ForwardKinematics\n",
    "from robobase.speedkinematics import SpeedKinematics"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### Координаты и скорости звеньев"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [],
   "source": [
    "joint_coords = [pi / 2, -pi / 2, pi / 2, pi / 3, pi / 2]\n",
    "joint_velocities = [0.1, 0.3, 0.2, -0.1, 0.6]"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### Параметры Денавита-Хартенберга манипулятора"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [],
   "source": [
    "theta1, theta2, theta3, theta4, theta5 = sy.symbols('theta1'), sy.symbols('theta2'), \\\n",
    "                                         sy.symbols('theta3'), sy.symbols('theta4'), \\\n",
    "                                         sy.symbols('theta5')\n",
    "\n",
    "r = [0, 5.75, 7.375, 0, 0]\n",
    "alpha = [-pi / 2, 0, 0, -pi / 2, 0]\n",
    "d = [3, 0, 0, 0, 4.125]\n",
    "thetas = [theta1, theta2 - pi / 2, theta3 + pi / 2, theta4 - pi / 2, theta5]\n",
    "\n",
    "args = {theta1: joint_coords[0], theta2: joint_coords[1], theta3: joint_coords[2],\n",
    "       theta4: joint_coords[3], theta5: [4]}"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### Формирование всех матриц преобразования T(i-1, i)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [],
   "source": [
    "links = ForwardKinematics.links(d, r, thetas, alpha)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### Формирование результирующей матрицы преобразования"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [],
   "source": [
    "result_matrix = ForwardKinematics.resultMatrix(links)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### Формирование матриц преобразования T(0, i) где i от 0 до N"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [],
   "source": [
    "frames = ForwardKinematics.frames(links)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### Расчет линейной скорости vx, vy, vz"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-0.36875    -1.42894192 -2.7875    ]\n"
     ]
    }
   ],
   "source": [
    "linear_speed = SpeedKinematics.linearSpeed(result_matrix, args, joint_velocities)\n",
    "\n",
    "print(linear_speed)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### Расчет угловой скорости wx, wy, wz"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-0.4         0.3        -0.41961524]\n"
     ]
    }
   ],
   "source": [
    "angle_speed = SpeedKinematics.angleSpeed(frames, args, joint_velocities)\n",
    "\n",
    "print(angle_speed)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}